package web.dao;

import ContentServer.extract.HtmlExtract;
import bean.SuccessfulNews;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;
import web.bean.Constants;
import web.bean.News;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 * Created by Administrator on 2016/7/1.
 */
@Repository
public class HomeDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    @Qualifier("jdbcTemplateYF")
    private JdbcTemplate jdbcTemplateYF;

    private HtmlExtract htmlExtract=null;


    public Map<String,Object> findNewsByDistrict(String districtNewsId,String province){
        String sql="select subscriber,subscriber_contact,subscriber_phone from tender_news_info_"+province+" where district_news_id=?";
        final Map<String,Object> map=new HashMap<String, Object>();
        map.put("subscriber","");
        jdbcTemplate.query(sql, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet rs) throws SQLException {
                map.put("subscriber",rs.getString("subscriber"));
                map.put("subscriber_contact",rs.getString("subscriber_contact"));
                map.put("subscriber_phone",rs.getString("subscriber_phone"));
            }
        },districtNewsId);
        return map;

    }


    public void findSuccessfulHtml(int location,final News news){
        if(htmlExtract==null){
            htmlExtract=new HtmlExtract();
        }
        String sql="select html from "+ Constants.CUT_SUCCESSFUL_TENDER_NEWS_HTML+"_"+location+" where successful_tender_news_id=?";
        jdbcTemplateYF.query(sql, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet rs) throws SQLException {
                //news.setContent(rs.getString("content"));
                news.setHtml(rs.getString("html"));
                try {
                    String content=htmlExtract.findNewsOfHtml(rs.getString("html"), news.getTitle());
                    news.setContent(content);
                } catch (IOException e) {
                    news.setContent("");
                    e.printStackTrace();
                }
            }
        },news.getId());
    }


}
